import { action, makeObservable, observable } from "mobx";
import { Bone } from "../animation/models/Bone";
import { emitter } from "../eventEmitter";
import { Transform } from "../animation/geom/Transform";

export type TransformData = {
    x: number;
    y: number;
    skew: number;
    rotation: number;
    scaleX: number;
    scaleY: number;
}

export type Mode = "transform" | "rotate" | "translate" | "scale";

export class TransformBarStore {
    transformData: TransformData;
    mode: Mode = "transform";

    constructor() {
        this.transformData = {
            x: 0.0,
            y: 0.0,
            skew: 0.0,
            rotation: 0.0,
            scaleX: 1.0,
            scaleY: 1.0,
        }
        makeObservable(this, {
            transformData: observable,
            mode: observable,
            setTransformData: action,
            setX: action,
            setY: action,
            setRotate: action,
            setScaleX: action,
            setScaleY: action,
            setMode: action,
        });
    }

    setTransformData(data: Bone) {
        this.transformData.x = +data.transform.x.toFixed(2);
        this.transformData.y = +data.transform.y.toFixed(2);
        this.transformData.skew = +data.transform.skew.toFixed(2);
        this.transformData.rotation = +(Transform.normalizeDegree(data.transform.rotation * Transform.RAD_DEG)).toFixed(2);
        this.transformData.scaleX = +data.transform.scaleX.toFixed(2);
        this.transformData.scaleY = +data.transform.scaleY.toFixed(2);
    }

    setX(x: number) {
        this.transformData.x = x;
        //emitter.emit("updateTransform", this.transformData);
    }

    setY(y: number) {
        this.transformData.y = y;
        //emitter.emit("updateTransform", this.transformData);
    }

    setRotate(rotation: number) {
        this.transformData.rotation = rotation;
        //emitter.emit("updateTransform", this.transformData);
    }

    setScaleX(scale: number) {
        this.transformData.scaleX = scale;
    }

    setScaleY(scale: number) {
        this.transformData.scaleY = scale;
    }

    setMode(mode: Mode) {
        this.mode = mode;
    }
}

export default new TransformBarStore();
